import type { ApiResponse, Course } from '../types/index';

export const courseApi = {
  async getCourses(params?: {
    category?: string;
    search?: string;
    page?: number;
    limit?: number;
  }): Promise<ApiResponse<Course[]>> {
    const query = new URLSearchParams();
    if (params?.category) query.append('category', params.category);
    if (params?.search) query.append('search', params.search);
    if (params?.page) query.append('page', params.page.toString());
    if (params?.limit) query.append('limit', params.limit.toString());
    
    const response = await fetch(`/api/courses?${query}`);
    return response.json();
  },

  async getCourse(id: number): Promise<ApiResponse<Course>> {
    const response = await fetch(`/api/courses/${id}`);
    return response.json();
  },

  async getPopularCourses(): Promise<ApiResponse<Course[]>> {
    const response = await fetch('/api/courses/popular');
    return response.json();
  },

  async getFreeCourses(): Promise<ApiResponse<Course[]>> {
    const response = await fetch('/api/courses/free');
    return response.json();
  },

  async getCategories(): Promise<ApiResponse<string[]>> {
    const response = await fetch('/api/courses/categories');
    return response.json();
  }
};